home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FishMarket 1.0
/
FishMarket v1.0.iso
/
fishies
/
126-150
/
disk_126
/
dance
/
dance1.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-06
|
6KB
|
263 lines
/****************************************************************
* Polygon drawing demo by John M. Olsen
* This demo uses the AreaMove, AreaDraw and AreaEnd Functions.
*
* My appologies for the sparceness of comments. It is hopefully clear
* what I was doing for the most part.
*
* This is meant to be compiled with the Manx compiler, and has not been
* tried under Lettuce.
*
* This software is in the public domain, and you can do whatever you want
* to or with it.
*
* John M. Olsen
* 1547 Jamestown Drive
* Salt Lake City, UT 84121
****************************************************************/
#include <exec/types.h>
#include <graphics/gfxbase.h>
#include <graphics/gfxmacros.h>
#include <graphics/rastport.h>
#include <intuition/intuition.h>
#include <stdio.h>
void *OpenLibrary();
struct Window *OpenWindow(), *w, *w2;
struct Screen *OpenScreen(), *s;
struct IntuiMessage *GetMsg(), *msg;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
BYTE *AllocRaster();
WORD areabuffer[250];
struct TmpRas tmpras;
struct AreaInfo myAreaInfo;
USHORT a[] = { 0x9429, 0x4294 };
USHORT b[] = { 0x4294, 0x2942 };
/*****************************************************************
definition of the 320 X 200 screen.
*****************************************************************/
struct NewScreen ss =
{ /*****************/
0, /* LeftEdge */
0, /* TopEdge */
320, /* Width */
200, /* Height */
5, /* Depth */
0, /* DetailPen */
1, /* BlockPen */
NULL, /* ViewModes */
CUSTOMSCREEN, /* Type */
NULL, /* *Font */
(UBYTE *) "A Dancing Polygon", /* *DefaultTitle */
NULL, /* *Gadgets */
NULL /* *CustomBitMap */
}; /*****************/
struct NewWindow ww =
{ /****************/
0, /* LeftEdge */
10, /* TopEdge */
320, /* Width */
190, /* Height */
-1, /* DetailPen */
-1, /* BlockPen */
CLOSEWINDOW, /* IDCMP */
WINDOWCLOSE | ACTIVATE | NOCAREREFRESH | WINDOWDRAG,
NULL, /* *FirstGadget */
NULL, /* *CheckMark */
(UBYTE *)"By John M. Olsen", /* *Title */
NULL, /* Screen */
NULL, /* *Bitmap */
0,0,0,0, /* Min/Max w,h */
CUSTOMSCREEN /* Type */
};
struct NewWindow ww2 =
{ /****************/
0, /* LeftEdge */
0, /* TopEdge */
320, /* Width */
190, /* Height */
-1, /* DetailPen */
-1, /* BlockPen */
NULL, /* IDCMP */
BACKDROP | NOCAREREFRESH,
NULL, /* *FirstGadget */
NULL, /* *CheckMark */
(UBYTE *)NULL, /* *Title */
NULL, /* Screen */
NULL, /* *Bitmap */
0,0,0,0, /* Min/Max w,h */
CUSTOMSCREEN /* Type */
};
SHORT cmap[32] =
{
0x000, 0xfff, 0xffd, 0xffb, 0xff9, 0xef8, 0xdf7, 0xde7,
0xdc7, 0xda7, 0xd87, 0xb87, 0xb85, 0xb83, 0xa84, 0x985,
0x886, 0x877, 0x857, 0x958, 0xa48, 0xb38, 0x938, 0x738,
0x648, 0x558, 0x468, 0x369, 0x36b, 0x36d, 0x45d, 0x55e
};
main(argc,argv)
int argc;
char *argv[];
{
setup();
drawstuff();
die(0);
}
setup()
{
if(!(GfxBase = OpenLibrary("graphics.library",0l)))
die(1);
if(!(IntuitionBase = OpenLibrary("intuition.library", 0l)))
die(2);
if(!(s = OpenScreen(&ss)))
die(4);
ww.Screen = s;
ww2.Screen = s;
if(!(w = OpenWindow(&ww2)))
die(5);
if(!(w2 = OpenWindow(&ww)))
die(6);
/* Set up a buffer for the AreaMove, AreaDraw, and AreaEnd commands */
InitArea(&myAreaInfo, areabuffer, 100l);
w->RPort->AreaInfo = &myAreaInfo;
tmpras.RasPtr = (BYTE *) AllocRaster(320l, 200l);
tmpras.Size = (long) RASSIZE(320l, 200l);
w->RPort->TmpRas = &tmpras;
LoadRGB4(&s->ViewPort, cmap, 32L);
}
drawstuff()
{
struct RastPort *r;
register long loop;
long color, x[10], y[10], dx[10], dy[10];
for(loop = 0l; loop < 5l; loop++)
{ /* init the position */
x[loop] = (long)random(w->Width - w->BorderLeft
- w->BorderRight) + w->BorderLeft;
y[loop] = (long)random(w->Height - w->BorderTop
- w->BorderBottom) + w->BorderTop;
dx[loop] = dy[loop] = 0L;
}
r = w->RPort;
msg = GetMsg(w2->UserPort);
while(msg->Class != CLOSEWINDOW)
{
if(msg)
ReplyMsg(msg);
msg = GetMsg(w2->UserPort);
/* draw with 2 colors this time, and just one the next to */
/* get all 3 colors modified. */
SetDrMd(r, JAM2);
color += random(3) - 1;
if(color < 1L) color = 1L;
if(color > 31L) color = 31L;
SetAPen(r, color);
/* Do a 5 point polygon */
for(loop = 0l; loop < 5l; loop++)
{
dx[loop] += (long)random(3) - 1L;
dy[loop] += (long)random(3) - 1L;
if(dy[loop] > 5L) dy[loop] = 5L;
if(dx[loop] > 5L) dx[loop] = 5L;
if(dy[loop] < -5L) dy[loop] = -5L;
if(dx[loop] < -5L) dx[loop] = -5L;
if(x[loop] + dx[loop] > w->Width - w->BorderRight
|| x[loop] + dx[loop] < w->BorderLeft)
dx[loop] *= -1;
if(y[loop] + dy[loop] > w->Height - w->BorderBottom
|| y[loop] + dy[loop]< w->BorderTop)
dy[loop] *= -1;
x[loop] += dx[loop];
y[loop] += dy[loop];
if(x[loop] > w->Width - w->BorderRight)
x[loop] = w->Width - w->BorderRight;
if(x[loop] < w->BorderLeft)
x[loop] = w->BorderLeft;
if(y[loop] > w->Height - w->BorderBottom)
y[loop] = w->Height - w->BorderBottom;
if(y[loop] < w->BorderTop)
y[loop] = w->BorderTop;
if(loop == 0l)
AreaMove(r, x[loop], y[loop]);
else
AreaDraw(r, x[loop], y[loop]);
}
AreaEnd(r);
WaitTOF();
ClipBlit(r, 0L, 10L, w2->RPort, 0L, 10L,
(long)w->Width, (long)w->Height-10L, 0xc0L);
}
if(msg)
ReplyMsg(msg);
}
/****************************************************************
* I don't think this random number will set any speed records, but
* it is guaranteed to be as random as you can get.
****************************************************************/
random(max)
int max;
{
static ULONG num;
ULONG sec, mic;
CurrentTime(&sec, &mic);
num *= sec;
num += mic;
while(num > 32000)
num = num >> 1;
return((int)(num % (ULONG)max));
}
die(kind)
int kind;
{
static char *msgs[] = { "",
/* err 1 */ "Unable to open graphics library.",
/* err 2 */ "Unable to open intuition library.",
/* err 3 */ "Unable to open a window.",
"screen failed.",
"window 1 failed.",
"window 2 failed."
};
if(kind) puts(msgs[kind]);
if(tmpras.RasPtr) FreeRaster(tmpras.RasPtr,320l,200L);
if(w) CloseWindow(w);
if(w2) CloseWindow(w2);
if(s) CloseScreen(s);
if(GfxBase) CloseLibrary(GfxBase);
if(IntuitionBase) CloseLibrary(IntuitionBase);
exit(kind);
}